/*
 * @FileName: [DictionaryAppController.java] 
 * @Package com.bci.pwtz.controller.app 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ALUMNI
 * @version 1.1
 * @author $Author$
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bci.pwtz.controller.app;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bci.pwtz.common.remote.BaseRequest;
import com.bci.pwtz.common.remote.ListResponse;
import com.bci.pwtz.common.remote.ObjResponse;
import com.bci.pwtz.common.remote.ReturnCode;
import com.bci.pwtz.common.remote.req.DictionaryReq;
import com.bci.pwtz.controller.AbstractController;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.model.Dictionary;
import com.bci.pwtz.service.DictionaryService;

/**
 * 一句话描述 <br/>
 * <p>Description:<br/>
 *    <p>TODO 详细描述
 *
 * <p>For Examples <br/>
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/api/dictionary")
public class DictionaryAppController extends AbstractController
{
    // 自动扫面
    @Autowired
    private DictionaryService dictionaryService;
    
    @RequestMapping(value="/add",method=RequestMethod.POST)
    @ResponseBody
    public ObjResponse<Dictionary> add(HttpServletRequest request){
        DictionaryReq req=null;
        ObjResponse<Dictionary> resp=new ObjResponse<Dictionary>();
        try{
            req=getJsonToObj(request,DictionaryReq.class);
            if(req==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("请求为空错误");
                return resp;
            }
            if(req.getObj()==null){
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(req.getSecretKey()==null){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥为空");
                return resp;
            }
            if(req.getTimestamp()==null){
                resp.setReturnCode(ReturnCode.OTHER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if(!validateSecretKey(req.getSecretKey(),req.getTimestamp())){
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("秘钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(StringUtils.isBlank(req.getToken())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if(!validateToken(req.getToken(), req.getUserId())){
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            int result=0;
            Dictionary dictionary=req.getObj();
            dictionary.setStatus(2);
            result=dictionaryService.add(dictionary);
            if(result>0){
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("添加数据字典成功");
                resp.setTimestamp(req.getTimestamp());
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("添加数据字典调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }catch(PwtzDatabaseException e){
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }catch(Exception e){
            resp.setReturnCode(ReturnCode.OTHER_ERROR);
            resp.setReturnMsg("系统错误");
            req.setTimestamp(resp.getTimestamp());
        }
        return resp;
    }
    /**
     * 查看数据字典方法
     * 一句话描述 <br/>
     * <p>Description: TODO(详细描述) 
     * <br/>
     * <p>Author: Administrator<br/>
     * <p>Date: 2014年9月26日-上午11:28:52<br/>
     * <p>
     * @param request
     * @return   
     *
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public ListResponse<Dictionary> list(HttpServletRequest request)
    {
        BaseRequest req = null;
        ListResponse<Dictionary> resp=new ListResponse<Dictionary>();
        try
        {
            req = (BaseRequest) getJsonToObj(request, BaseRequest.class);
            if (req.getTimestamp() == null)
            {
                resp.setReturnCode(ReturnCode.PARAMETER_ERROR);
                resp.setReturnMsg("参数错误");
                return resp;
            }
            if (req.getSecretKey() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (!validateSecretKey(req.getSecretKey(), req.getTimestamp()))
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("密钥错误");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (req.getUserId() == null)
            {
                resp.setReturnCode(ReturnCode.SECRET_KEY_ERROR);
                resp.setReturnMsg("用户ID为空");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            if (StringUtils.isEmpty(req.getToken()) || !validateToken(req.getToken(), req.getUserId()))
            {
                resp.setReturnCode(ReturnCode.TOKEN_INVALID);
                resp.setReturnMsg("票据无效");
                resp.setTimestamp(req.getTimestamp());
                return resp;
            }
            List<Dictionary> list=dictionaryService.selectDictionary(null,null);
            if (list != null)
            {
                resp.setReturnCode(ReturnCode.SUCCESS);
                resp.setReturnMsg("查询数据词典信息成功");
                resp.setTimestamp(req.getTimestamp());
                resp.setObjss(list);
            }else{
                resp.setReturnCode(ReturnCode.INTERFACE_INVOKE_ERROR);
                resp.setReturnMsg("查询数据词典信息调用接口返回失败");
                resp.setTimestamp(req.getTimestamp());
            }
        }
        catch (PwtzDatabaseException e)
        {
            resp.setReturnCode(ReturnCode.DB_EXCEPTION);
            resp.setReturnMsg("数据库操作异常");
            resp.setTimestamp(req.getTimestamp());
        }
        catch (Exception e)
        {
            resp.setReturnCode(ReturnCode.UNKOWN_ERROR);
            resp.setReturnMsg("系统错误");
            resp.setTimestamp(req.getTimestamp());
        }
        return resp;
    }
}
